"""
分页
"""
import math


class Pagination:
    def __init__(self, current_page, all_count, base_url, query_params, row_count=15, button_count=7):
        """
        :param current_page: 当前页码
        :param row_count: 每页显示多少行数据
        :param all_count: 数据库中总数据行
        :param base_url: 基础 URL
        :param query_params: QueryDict对象，内部含当前 URL的所有参数
        :param button_count: 页面上最多显示的页码数量
        """
        self.base_url = base_url
        try:
            self.current_page = int(current_page)
            if self.current_page <= 0:
                raise Exception()
        except Exception as e:
            self.current_page = 1
        self.query_params = query_params
        self.row_count = row_count
        self.all_count = all_count
        self.button_count = button_count
        self.pages_count = math.ceil(all_count / row_count)  # 总页数

        half_button_count = int(button_count / 2)
        self.half_button_count = half_button_count

    @property
    def start(self):
        return (self.current_page - 1) * self.row_count

    @property
    def end(self):
        return self.current_page * self.row_count

    def page_html(self):
        if self.pages_count == 0:
            return None
        if self.pages_count < self.button_count:  # 总页数 小于 基准页数
            page_start = 1  # 起始页码
            page_end = self.pages_count  # 终止页码
        else:
            if self.current_page <= self.half_button_count:
                page_start = 1
                page_end = self.button_count
            else:
                if (self.current_page + self.half_button_count) > self.pages_count:
                    page_end = self.pages_count
                    page_start = self.pages_count - self.button_count + 1
                else:
                    page_start = self.current_page - self.half_button_count
                    page_end = self.current_page + self.half_button_count

        page_list = []

        if self.current_page <= 1:
            prev = '<li><a href="#">上一页</a></li>'
        else:
            self.query_params['page'] = self.current_page - 1
            prev = '<li><a href="%s?%s">上一页</a></li>' % (self.base_url, self.query_params.urlencode())
        page_list.append(prev)
        self.query_params['page'] = 1
        first_page = '<li><a href="%s?%s">首页</a></li>' % (self.base_url, self.query_params.urlencode())
        page_list.append(first_page)

        for i in range(page_start, page_end + 1):
            self.query_params['page'] = i
            if self.current_page == i:
                tmp = '<li class="active"><a href="%s?%s">%s</a></li>' % (self.base_url, self.query_params.urlencode(), i)
            else:
                tmp = '<li><a href="%s?%s">%s</a></li>' % (self.base_url, self.query_params.urlencode(), i)

            page_list.append(tmp)

        self.query_params['page'] = self.pages_count
        last_page = '<li><a href="%s?%s">末页</a></li>' % (self.base_url, self.query_params.urlencode())
        page_list.append(last_page)

        if self.current_page >= self.pages_count:
            nex = '<li><a href="#">下一页</a></li>'
        else:
            self.query_params['page'] = self.current_page + 1
            nex = '<li><a href="%s?%s">下一页</a></li>' % (self.base_url, self.query_params.urlencode())
        page_list.append(nex)
        page_str = "".join(page_list)

        return page_str
